<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionAction' );
import( 'de.ceus-media.framework.krypton.exception.IO' );
import( 'classes.action.panel.EnvironmentSelector' );
import( 'classes.logic.Topic' );
import( 'classes.logic.Case' );
/**
 *	Option Actions.
 *	@package		mv2.action.management
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_Licencee
 *	@uses			Action_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Actions.
 *	@package		mv2.action.management
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_Licencee
 *	@uses			Action_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class Action_Management_Structure extends Framework_Krypton_Core_DefinitionAction
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->loadLanguage( 'management.structure', 'structure' );
		$this->addAction( 'addCase' );
		$this->addAction( 'addTopic' );
		$this->addAction( 'editCase' );
		$this->addAction( 'editTopic' );
		$action	= new Action_Panel_EnvironmentSelector();
	}
	
	protected function addCase()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$session	= Framework_Krypton_Core_Registry::getStatic( "session" );
		$words		= $this->words['structure']['msg'];

		$projectId	= $session->get( 'projectId' );
		try
		{
			$logic		= new Logic_Case();
			if( !$logic->addCase( $request->getAll(), 'add_case_' ) )
				return $this->messenger->noteError( $words['error_case_not_added'] );
			$this->removeCachedTree( $projectId );
			$this->messenger->noteSuccess( $words['success_case_added'] );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'structure', 'addCase' );
		}
	}
	
	protected function addTopic()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$session	= Framework_Krypton_Core_Registry::getStatic( "session" );
		$words		= $this->words['structure']['msg'];

		$projectId	= $session->get( 'projectId' );
		try
		{
			$data	= $request->getAll();
 			$data['add_topic_project_id']	= $session->get( 'projectId' );
			$logic		= new Logic_Topic();
			if( !$logic->addTopic( $data, 'add_topic_' ) )
				return $this->messenger->noteError( $words['error_topic_not_edited'] );
			$this->removeCachedTree( $projectId );
			$this->messenger->noteSuccess( $words['success_topic_edited'] );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'structure', 'addTopic' );
		}
	}
	
	protected function editCase()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$session	= Framework_Krypton_Core_Registry::getStatic( "session" );
		$words		= $this->words['structure']['msg'];

		$projectId	= $session->get( 'projectId' );
		$caseId		= $request->get( 'caseId' );
		if( $caseId )
		{
			try
			{
				$logic	= new Logic_Case();
				if( !$logic->editCase( $caseId, $request->getAll(), 'edit_case_' ) )
					return $this->messenger->noteError( $words['error_case_not_edited'] );
				$this->removeCachedTree( $projectId );
				$this->messenger->noteSuccess( $words['success_case_edited'] );
			}
			catch( Exception $e )
			{
				$this->handleException( $e, 'structure', 'editCase' );
			}
		}
	}
	
	protected function editTopic()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$session	= Framework_Krypton_Core_Registry::getStatic( "session" );
		$words		= $this->words['structure']['msg'];

		$projectId	= $session->get( 'projectId' );
		$topicId	= $request->get( 'topicId' );
		if( $topicId )
		{
			try
			{
				$logic	= new Logic_Topic();
				if( !$logic->editTopic( $topicId, $request->getAll(), 'edit_topic_' ) )
					return $this->messenger->noteError( $words['error_topic_not_edited'] );
				$this->removeCachedTree( $projectId );
				$this->messenger->noteSuccess( $words['success_topic_edited'] );
			}
			catch( Exception $e )
			{
				$this->handleException( $e, 'structure', 'editTopic' );
			}
		}
	}
	
	private function removeCachedTree( $projectId )
	{
		$config		= Framework_Krypton_Core_Registry::getStatic( "config" );
		$cacheFile	= $config['paths']['cache']."optionTreeAdmin_".$projectId.".cache";
		@unlink( $cacheFile );
	}
}
?>
